﻿using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;

namespace wt.wine.Application.ManageUserTable.Queries
{
    /// <summary>
    /// 员工管理列表
    /// </summary>
    public class GetUserListQueries : IRequest<Json<PaginatedList<GetUserTableByIdDTO>>>
    {
        public int pageIndex { get; set; }
        public int pageSize { get; set; }
    }

    public class GetUserListQueriesHandler : IRequestHandler<GetUserListQueries, Json<PaginatedList<GetUserTableByIdDTO>>>
    {
        private readonly ICurrentUserService _icurrentUserServices;
        private readonly IUsertableRepository _iuserTableRepository;
        private readonly IUserdevicetableRepository _iuserdevicetableRepository;
        private readonly IDevicetableRepository _idevicetableRepository;
        public GetUserListQueriesHandler(ICurrentUserService icurrentUserServices, IUsertableRepository iuserTableRepository, IUserdevicetableRepository iuserdevicetableRepository, IDevicetableRepository devicetableRepository)
        {
            _icurrentUserServices = icurrentUserServices;
            _iuserTableRepository = iuserTableRepository;
            _iuserdevicetableRepository = iuserdevicetableRepository;
            _idevicetableRepository = devicetableRepository;
        }
        public async Task<Json<PaginatedList<GetUserTableByIdDTO>>> Handle(GetUserListQueries request, CancellationToken cancellationToken)
        {
            int userid = _icurrentUserServices.GetUserId();
            PaginatedList<GetUserTableByIdDTO> model = new PaginatedList<GetUserTableByIdDTO>(new List<GetUserTableByIdDTO>() { }, 0, 0, 0);
            if (request.pageIndex <= 0 || request.pageSize > 1000)
            {
                return Json<PaginatedList<GetUserTableByIdDTO>>.Failed("参数不能为空", model);
            }
            else
            {
                int roletype = _icurrentUserServices.GetRoletype();

                model = await _iuserTableRepository.GetUserList(userid, request.pageIndex, request.pageSize, cancellationToken);
                if (model != null && model.Items != null && model.Items.Any())
                {

                    List<GetUserTableDeviceList> list_deveicec = await _iuserdevicetableRepository.GetUserTableDeviceList(model.Items.Select(p => p.id).ToList(), cancellationToken);

                    for (int i = 0; i < model.Items.Count(); i++)
                    {
                        List<GetUserTableDeviceList> list_deviceuserid = new List<GetUserTableDeviceList>() { };
                        if (model.Items[i].type == 2)
                        {
                            var list_admindevice = await _idevicetableRepository.GetDeviceByAddUser(model.Items[i].id, cancellationToken);
                            if (list_admindevice != null && list_admindevice.Any())
                            {
                                list_deviceuserid = list_admindevice.Select(p => new GetUserTableDeviceList()
                                {
                                    id = p.id,
                                    cusdeviceid = p.cusdeviceid,
                                    deviceid = p.deviceid,
                                    skuname = p.skuname,
                                    userid = model.Items[i].id
                                }).ToList();
                            }
                        }
                        else
                        {
                            if (list_deveicec != null && list_deveicec.Any())
                            {
                                list_deviceuserid= list_deveicec.Where(p => p.userid == model.Items[i].id).ToList();
                            }
                        }

                        model.Items[i].list = list_deviceuserid;
                    }
                }


                return Json<PaginatedList<GetUserTableByIdDTO>>.Success("操作成功", model);
            }
        }
    }
}
